import { HTTP } from "@/tools"

// 常规相应对象
export interface StandardResponse<T = any> {
  code: number
  data: T
  exceptionClazz?: any
  message: string
  msgId?: string
  sign?: string
  success: boolean
}
// 应用使用场景
export type ApplicationClientType = 'PC' | 'IPAD' | 'PHONE'
// 应用使用方
export type ApplicationUseType = 'MGR' | 'MCH' | 'MCO'

/**
 * 打开方式
 * 0,1,2,3=没有打开方式，组建，内部连接，外部连接
 */
export const enum OPENTYPE {
  NoThing = '0', Component = "1", InnerLink = '2', OuterLink = '3'
}
/**
 * 菜单类型
 * 0,1,2=目录,菜单,按钮
 */
export const enum MENUTYPE {
  DIR = '0', MENU = "1", BUTTON = '2'
}
/**
 * 业务权重
 * 超级管理员只能看到
 */
export const enum BUSNISSTYPE {
  SYSTEM = '1',
  SERVICE = '2'
}
export interface StandardEntity {
  createTime: string | number,
  createUser: string,
  updateTime: string,
  updateUser: string | number,
  status?: 0 | 1 | 2,
}
export interface DataRule {
  ruleColumn: string,
  ruleCondition: string,
  ruleValue: string
}

export interface PagingQuery {
  pageNo: number,
  pageSize: number,
}
export interface PermissionObj {
  dataOrgScope: string[],
  dataRules: DataRule[],
  dataScope: number[],
  relevancyId: string,
  searchBeginTime: string,
  searchEndTime: string,
  searchStatus: number,
  searchValue: string,
  singleScope: true,
  sysType: ApplicationUseType,
  tenantId: string,
}
/**
 * 常规查询对象
 */
export interface StandardQuery extends PagingQuery, PermissionObj { }

/**
 * 分页接口常规相应结构
 */
export interface StandardPagingResult<T = any> {
  offlineTotal: number,
  onlineTotalGoods: number,
  pageNo: number,
  pageSize: number,
  rainbow: number[],
  rows: T[],
  totalPage: number,
  totalRows: number
}

/**
 * 通用分页函数
 * 抽离目的:减少类型重复定义问题
 * @param url 
 * @param params 
 * @returns 
 */
export async function GetDataByPaging<ReturnType = any, QueryType extends Partial<StandardQuery> = any>(url: string, params?: QueryType) {
  return HTTP.post<StandardResponse<StandardPagingResult<ReturnType>>>(url, params)
}

/**
 * 专为proTable设计的特殊分页请求工具函数
 * 解决传参以及回参的转换问题
 * @param url 
 * @param params 
 * @returns 
 */
export async function GetData2ProTable<ReturnType = any, QueryType extends Partial<StandardQuery> & { current?: number } = any>(url: string, params: QueryType) {
  const { current, ...o } = params
  return GetDataByPaging<ReturnType, Omit<QueryType, 'current'>>(url, { ...o, pageNo: current })
    .then(({ data }) => ({ data: data.data.rows, total: data.data.totalRows }))
}